﻿{extend name="public:base" /}{block name="css"}
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css"/>
<link rel="stylesheet" type="text/css" href="__ADMIN__/css/style.css">{/block}{block name="content"}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-content">
                    <form class="layui-form" method="post">
                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">名称</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="username" placeholder="请输入名称" value="{$info.username}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        {if session('id') == 1 || $user_type == 0}
                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">渠道/团队长</label>
                                <div class="col-sm-3">
                                    <select name="up_company_user_id" id="companySelect" lay-filter="companySelect" lay-verify="required" class="layui-select">
                                        <option value="0">请选择</option>
                                        {volist name="company_list" id="vo"}
                                        <option value="{$vo.id}" {$info.up_company_user_id== $vo.id ? 'selected' : ''}>{$vo.username}</option>
                                        {/volist}
                                    </select>
                                </div>
                                <div class="col-sm-3">
                                    <select name="up_channel_user_id" id="channelSelect" class="layui-select">
                                        <option value="0">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        {/if}
                        {if $user_type == 2}
                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">渠道/团队长</label>
                                <div class="col-sm-3">
                                    <select name="up_channel_user_id" lay-verify="required" class="layui-select">
                                        <option value="0">请选择</option>
                                        {volist name="channel_list" id="vo"}
                                        <option value="{$vo.id}" {$info.up_channel_user_id== $vo.id ? 'selected' : ''}>{$vo.username}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        {/if}

                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">账号</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="account" placeholder="请输入账号" value="{$info.account}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        {if !input('id')}
                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">密码</label>
                                <div class="col-sm-3">
                                    <input type="password" class="form-control" name="password" placeholder="请输入密码" value="{:thinkDecrypt($info.password)}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">确认密码</label>
                                <div class="col-sm-3">
                                    <input type="password" class="form-control" name="re_password" placeholder="请确认密码" value="{:thinkDecrypt($info.password)}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        {/if}

                        <input type="hidden" name="id" value="{:input('id')}">
                        <input type="hidden" id="initialChannelId" value="{$info.up_channel_user_id|default=0}">

                        <div class="form-group row">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button type="button" class="btn btn-w-m btn-default back-page">返回</button>
                                <button type="button" class="btn btn-w-m btn-primary ajax-post">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name='js'}
<script src="__STATIC__/layui/layui.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        // 公司列表数据
        var company_list = {$company_list_json};
        var initialChannelId = $('#initialChannelId').val();

        // 初始化函数 - 在页面加载时执行
        function initForm() {
            // 获取初始选中的公司ID
            var initialCompanyId = $('#companySelect').val();

            if (initialCompanyId && initialCompanyId > 0) {
                // 初始化渠道下拉框
                loadChannels(initialCompanyId, initialChannelId);
            }
        }

        // 根据公司ID加载渠道数据
        function loadChannels(companyId, selectedChannelId) {
            // 清空渠道下拉框
            $('#channelSelect').empty().append('<option value="0">请选择</option>');

            // 查找对应的公司
            var company = company_list.find(function (item) {
                return item.id == companyId;
            });

            // 如果找到公司且有渠道列表
            if (company && company.channel_list && company.channel_list.length > 0) {
                // 添加渠道选项
                company.channel_list.forEach(function (channel) {
                    var option = $('<option></option>')
                      .val(channel.id)
                      .text(channel.username);

                    // 如果是编辑页面且当前渠道ID匹配，设置为选中
                    if (selectedChannelId && channel.id == selectedChannelId) {
                        option.attr('selected', 'selected');
                    }

                    $('#channelSelect').append(option);
                });
            }

            // 重新渲染select
            form.render('select');
        }

        // 监听公司选择变化事件 - 这是Layui推荐的标准方式
        form.on('select(companySelect)', function (data) {
            var companyId = data.value;
            loadChannels(companyId, 0);
        });

        // 确保表单正确渲染
        form.render();

        // 页面加载完成后初始化表单
        $(function () {
            initForm();
        });
    });
</script>{/block}